//
//  DMPersonIntroVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/17.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMPersonIntroVC.h"
#import "DMNicknameVC.h"

//view
#import "KChooseImageControlView.h"
//request
#import "KUploadImageRequest.h"
#import "KUserControlRequest.h"

@interface DMPersonIntroVC ()

@property (weak, nonatomic) IBOutlet UIImageView *haedImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLb;
@property (weak, nonatomic) IBOutlet UIButton *maleBt;
@property (weak, nonatomic) IBOutlet UIButton *femaleBt;
@property (weak, nonatomic) IBOutlet UILabel *phoneLb;



@end

@implementation DMPersonIntroVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
    [self.maleBt addTarget:self action:@selector(changeSexActionWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.femaleBt addTarget:self action:@selector(changeSexActionWithButton:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setData];
}

-(void)startInit {
    
    self.navigationItem.title = @"编辑个人资料";
    self.view.backgroundColor = DMDefaultColor;
}



-(void)setData{
    [self.haedImage sd_setImageWithURL:[NSURL URLWithString:[KuserInfo shareInstance].userModel.profile] placeholderImage:[UIImage imageNamed:@"moren_touxiang"]];
    self.nameLb.text = [KuserInfo shareInstance].userModel.nickName;
    
    if ([[KuserInfo shareInstance].userModel.sex isEqualToString:@"1"]) {
        //男
        [_maleBt setTitleColor:DMWhiteColor forState:UIControlStateNormal];
        _maleBt.backgroundColor = DMMainColor;
        [_femaleBt setTitleColor:DMFontColor forState:UIControlStateNormal];
        _femaleBt.backgroundColor = DMWhiteColor;
    }else{
        
        //女
        [_maleBt setTitleColor:DMFontColor forState:UIControlStateNormal];
        _maleBt.backgroundColor = DMWhiteColor;
        [_femaleBt setTitleColor:DMWhiteColor forState:UIControlStateNormal];
        _femaleBt.backgroundColor = DMMainColor;
        
    }
    
    
    self.phoneLb.text = [KuserInfo shareInstance].userModel.phone;
}

- (IBAction)buttonClick:(UIButton *)sender {
    if (sender.tag == 1) {
        //头像
        MJWeakSelf
        [KChooseImageControlView showChooseImageControllerWithBaseVC:self imageCount:1 SureBlock:^(NSArray<UIImage *> *imageArr) {
            if (imageArr.count>0) {
                [weakSelf uploadBGImage:imageArr[0]];
            }
        }];

    }else if (sender.tag == 2) {
        //昵称
        DMNicknameVC *vc = [DMNicknameVC new];
        MJWeakSelf
        [vc setReloadBlock:^{
            [weakSelf setData];
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (sender.tag == 3) {
        //男
        [_maleBt setTitleColor:DMWhiteColor forState:UIControlStateNormal];
        _maleBt.backgroundColor = DMMainColor;
        [_femaleBt setTitleColor:DMFontColor forState:UIControlStateNormal];
        _femaleBt.backgroundColor = DMWhiteColor;
    }else if (sender.tag == 4) {
        //女
        [_maleBt setTitleColor:DMFontColor forState:UIControlStateNormal];
        _maleBt.backgroundColor = DMWhiteColor;
        [_femaleBt setTitleColor:DMWhiteColor forState:UIControlStateNormal];
        _femaleBt.backgroundColor = DMMainColor;
    }
}


#pragma mark ----netWork----
-(void)uploadBGImage:(UIImage *)image{
    KUploadImageRequest * request = [[KUploadImageRequest alloc]initWithUploadImage:image];
    
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    //上传图片
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        NSDictionary * dic = [NSDictionary dictionaryWithDictionary:baseModel.data];
        
        NSString * imageURL = [dic objectForKey:@"imageUrl"];
        KUserControlRequest * controlRequest = [[KUserControlRequest alloc]initChangeUserInfoWithNickName:nil profile:imageURL sex:nil];
        
        //修改头像
        [controlRequest newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            
            [JLProgressHUDManager showSuccessHud:@"上传头像成功"];
            
            [[KuserInfo shareInstance]updateUserModel:^(KUserModel *userModel) {
                [weakSelf setData];
            } failure:nil];

            

        } failure:^(KBaseModel *errorModel) {
            
            [JLProgressHUDManager showErrorHud:errorModel.msg];
            
        }];
        
        
    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];
        
    }];
    
}

-(void)changeSexActionWithButton:(UIButton *)btn{

    NSString *sexTypeStr;

    if (btn.tag == 3) {
        sexTypeStr = @"1";

    }else{
        sexTypeStr = @"2";

    }
    
    
    KUserControlRequest * controlRequest = [[KUserControlRequest alloc]initChangeUserInfoWithNickName:nil profile:nil sex:sexTypeStr];
    
    //修改性别
    MJWeakSelf
    [controlRequest newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        [JLProgressHUDManager showSuccessHud:@"修改性别成功"];
        
        [KuserInfo shareInstance].userModel.sex = sexTypeStr;
        [[KuserInfo shareInstance] archiveUserInfo];
        [weakSelf setData];
        
    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];
        
    }];
    
}

@end
